package cn.t.converter.order;

import cn.t.core.converter.CoreDTOConverter;
import cn.t.dto.device.CargowaySkuDTO;
import cn.t.dto.device.vo.OpenCloseDTO;
import cn.t.dto.order.OrderDetailDTO;
import cn.t.model.order.OrderDetailDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 领料订单明细表(OrderDetail)dto转domain
 *
 * @author t
 * @since 2024-05-24 10:19:34
 */
@Mapper(componentModel = "spring")
public interface OrderDetailDTOConverter extends CoreDTOConverter<OrderDetailDTO, OrderDetailDO> {
    List<OrderDetailDO> dto2DoList(List<OrderDetailDTO> orderDetails);
    @Mapping(target = "stdWeight",expression = "java(cargowaySkuDO.getSkuWeight().doubleValue())")
    @Mapping(target = "id",ignore = true)
    OrderDetailDO cargowaySku2Do(CargowaySkuDTO cargowaySkuDO);

    default List<OrderDetailDO> toOderDetailList(OpenCloseDTO openCloseDO){
        Map<Integer, Double> openScale = openCloseDO.getOpenScale();
        String orderSn = openCloseDO.getOrderSn();

        Map<Integer, Double> closeScale = openCloseDO.getScaleMap();
        List<OrderDetailDO> detailList = openCloseDO.getQtyList().stream().filter(qtyVO -> qtyVO.getErrorTypeEnum() == null).map(qtyVO -> {
            CargowaySkuDTO cargowaySkuDO = openCloseDO.getCargowaySkuMap().get(qtyVO.getNo());
            OrderDetailDO orderDetailDO = cargowaySku2Do(cargowaySkuDO);
            orderDetailDO.setOrderSn(orderSn);
            Integer qty = qtyVO.getQty();
            //正常领用 种类是负数，但是放回变成正式，数量这里区分下,统计做一下过滤（相同商品一个货道放在另外一个货道）
            if (qtyVO.getWeight() > 0) {
                qty = qty * -1;
            }
            orderDetailDO.setQty(qty);
            orderDetailDO.setBeforeWeight(openScale.get(qtyVO.getNo()));
            orderDetailDO.setAfterWeight(closeScale.get(qtyVO.getNo()));
            return orderDetailDO;
        }).collect(Collectors.toList());
return detailList;
    }
}

